/*
 * @Author: chenr 526963089@qq.com
 * @Date: 2023-02-13 09:39:03
 * @LastEditors: chenr 526963089@qq.com
 * @LastEditTime: 2023-02-16 18:11:28
 * @Description:
 */
import Mock, { Random } from 'mockjs';
import { builder, getBody } from '../../util';
import type {
  CaptchaInterface,
  LoginParamsType,
  LoginInterface,
  MenusInterface,
} from '@/api/mock/types';

/**
 * @description: 获取图形验证码
 * @return {*}
 */
const captcha = () => {
  const number = Mock.Random.string('number', 4);
  return builder<CaptchaInterface>({
    data: {
      key: '4291d7da9005377ec9aec4a71ea837f',
      image: Mock.Random.image('200x34', '#50B347', '#FFF', number),
    },
    code: 200,
  });
};

/**
 * @description: 登陆
 * @param {LoginParamsType} options
 * @return {*}
 */
const username = ['admin'];
const password = ['000000', 'Zr@123456.']; // admin
const login = (options: LoginParamsType) => {
  const body = getBody(options);
  if (body.username && body.password) {
    if (
      !username.includes(body.username) ||
      !password.includes(body.password)
    ) {
      return builder<LoginInterface | null>({
        data: null,
        message: '账户或密码错误',
        code: 401,
      });
    }
  }
  return builder<LoginInterface>({
    data: {
      access_token: Random.guid(),
      token_type: 'bearer',
      refresh_token: Random.guid(),
      expires_in: 5,
      scope: 'all',
      tenant_id: '000000',
      user_name: 'admin',
      real_name: '管理员',
      avatar:
        'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
      client_id: 'chiway_admin',
      role_name: 'administrator',
      license: 'powered by Chiway',
      post_id: '1123598817738675201',
      user_id: '1123598821738675201',
      role_id: '1123598816738675201',
      nick_name: '管理员',
      oauth_id: '',
      detail: {
        type: 'web',
      },
      dept_id: '1123598813738675201',
      account: 'admin',
      jti: 'e6df65e2-ef21-471c-b124-9936534c893e',
    },
    code: 200,
  });
};

/**
 * @description: 退出登录
 * @return {*}
 */
const logout = () => {
  return builder({ data: {}, message: '[测试接口] 注销成功' });
};

/**
 * @description: 用户菜单
 * @return {*}
 */
const menus = () => {
  return builder<MenusInterface[]>({
    data: [
      {
        id: '001',
        parentId: '0',
        code: 'home',
        name: '首页',
        alias: 'menu',
        path: '/home',
        component: 'Home',
        source: 'iconfont icon-spgl',
        sort: 1,
        category: 'MENU',
        remark: '',
      },
      {
        id: '002',
        parentId: '0',
        code: 'test',
        name: '主菜单',
        alias: 'menu',
        path: '/test',
        component: 'PageView',
        source: 'iconfont icon-spgl',
        sort: 1,
        category: 'MENU',
        remark: '',
        children: [
          {
            id: '002001',
            parentId: '002',
            code: 'test01',
            name: '子菜单01',
            alias: 'menu',
            path: '/test/01',
            component: 'Test01',
            source: '',
            sort: 1,
            remark: '',
          },
          {
            id: '002002',
            parentId: '002',
            code: 'test02',
            name: '子菜单02',
            alias: 'menu',
            path: '/test/02',
            component: 'Test02',
            source: '',
            sort: 2,
            remark: '',
          },
        ],
      },
      {
        id: '003',
        parentId: '0',
        code: 'page',
        name: '测试',
        alias: 'menu',
        path: '/page',
        component: 'PageView',
        source: 'iconfont icon-spgl',
        sort: 1,
        category: 'MENU',
        remark: '',
        children: [
          {
            id: '003001',
            parentId: '003',
            code: 'page01',
            name: '测试01',
            alias: 'menu',
            path: '/page/01',
            component: 'Test03',
            source: '',
            sort: 1,
            remark: '',
          },
          {
            id: '003002',
            parentId: '003',
            code: 'page02',
            name: '测试02',
            alias: 'menu',
            path: '/page/02',
            component: 'Test04',
            source: '',
            sort: 2,
            remark: '',
          },
        ],
      },
    ],
  });
};

export default () => {
  Mock.mock(/\/api\/mock\/user\/captcha/, 'get', captcha);
  Mock.mock(/\/api\/mock\/user\/login/, 'post', login);
  Mock.mock(/\/api\/mock\/user\/logout/, 'post', logout);
  Mock.mock(/\/api\/mock\/user\/menus/, 'get', menus);
};
